import CustomPage from './base/CustomPage'


const { init } = require('./../../wxCloudClientSDK.umd.js')
// 指定云开发环境 ID
wx.cloud.init({
  env: "cloud1-9gel6ndh8e555155", // 当前的云开发环境 ID
});
const client = init(wx.cloud);


Page({
  data: {
    isFavorite: false,
    cartCount: 2,
    serviceInfo: {
    },
    doctors: [
      {
        id: 1,
        name: '张医生',
        title: '主任医师',
        avatar: 'images/doctor.jpg'
      },
      {
        id: 2,
        name: '李医生',
        title: '副主任医师',
        avatar: 'images/doctor.jpg'
      }
    ],
    detailContent: '<div>详细的服务介绍内容...</div>',
    rating: 4.9,
    reviewCount: 2380,
    reviews: [
      {
        id: 1,
        userName: '张**',
        userAvatar: 'images/doctor.jpg',
        rating: 5,
        content: '医生很专业，服务很好，一点都不疼',
        time: '2024-03-10'
      },
      {
        id: 2,
        userName: '李**',
        userAvatar: 'images/doctor.jpg',
        rating: 5,
        content: '效果很好，以后还会再来',
        time: '2024-03-09'
      }
    ],
    showPopup: false,
    quantity: 1,
    
  },

  
  onLoad: function(options) {
    // options 包含传递过来的参数
    console.log("productid: " + options.param1);

    this.queryProductInfo(options.param1);
    
  },

  queryProductInfo(id) {
      // 获取商品信息
      client.models.product_info.get({
        filter: {
          where: {
          $and: [{
            productId: {
              $eq: id
            },
          },
        ],
          }
        },
      }).then(res => {
      console.log("product_info: " + res.data);
      this.parseProductInfo(res.data);
    }).catch(res => {
      console.log("product_info err: " + res)
    });
  },

  parseProductInfo(data) {
    const productInfo = {
      id: data._id,
      productId: data.productId,
      name: data.productName,
      price: data.price,
      originalPrice: 500,
      soldCount: 200,
      stock: 50,
      description: data.description,
      images: [data.images.url]
    };

    this.setData({
      serviceInfo: productInfo
    });
  },
  

  toggleFavorite() {
    this.setData({
      isFavorite: !this.data.isFavorite
    });
    wx.showToast({
      title: this.data.isFavorite ? '收藏成功' : '取消收藏',
      icon: 'success'
    });
  },

  selectSpec(e) {
    const id = e.currentTarget.dataset.id;
    const specifications = this.data.serviceInfo.specifications.map(spec => ({
      ...spec,
      selected: spec.id === id
    }));
    this.setData({
      'serviceInfo.specifications': specifications
    });
  },

  navigateToDoctor(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/doctors/detail?id=${id}`
    });
  },

  contactService() {
    wx.showToast({
      title: '正在接入客服...',
      icon: 'loading'
    });
  },

  navigateToCart() {
    wx.switchTab({
      url: '/pages/cart/index'
    });
  },

  addToCart() {

    const id = this.data.serviceInfo.productId;

    const timestamp = new Date("2025-03-06 20:40:35").getTime();

    const { _id } = client.models.cart.create({
      data: {
        quantity: 1,  // 数量
        productId: id,
        userId: 1,
        createTime: timestamp,
      },
    }).then(res => {

      console.log("save cart: " + res);

      
      wx.showToast({
        title: '添加成功',
        icon: 'success'
      });

      wx.switchTab({
        url: '/page/cart/index',
      });

    }).catch(res => {
        console.log("err save cart: " + res);
    });

    
    /*this.setData({
      cartCount: this.data.cartCount + 1
    });*/

    

  },

  buyNow() {
    wx.navigateTo({
      url: '/pages/checkout/index'
    });
  },

  // 显示购买弹出层
  showBuyPopup() {
    
    console.log("显示立即购买弹窗");


    this.setData({
      showPopup: true
    });
  },

  // 隐藏购买弹出层
  hideBuyPopup() {
    this.setData({
      showPopup: false
    });
  },

  // 减少数量
  decreaseQuantity() {
    if (this.data.quantity > 1) {
      this.setData({
        quantity: this.data.quantity - 1
      });
    }
  },

// 增加数量
increaseQuantity() {
  if (this.data.quantity < this.data.goodsInfo.stock) {
    this.setData({
      quantity: this.data.quantity + 1
    });
  }
},

  // 加入购物车
  /*addToCart() {
    // 这里添加加入购物车的逻辑
    wx.showToast({
      title: '已加入购物车',
      icon: 'success',
      duration: 2000
    });
    
    // 跳转到购物车页面
    wx.switchTab({
      url: '/pages/cart/cart'
    });
  },*/

  // 确认购买
  confirmBuy() {
    // 这里添加确认购买的逻辑
    wx.showToast({
      title: '购买成功',
      icon: 'success',
      duration: 2000
    });
    this.hideBuyPopup();
  },

}); 